<template>
  <div class='sub-category' v-if="subFilter">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter/>
           <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />
        <!-- 商品列表 -->
        <ul class="list">
          <li v-for="i in list" :key="i">
            <GoodsItem :goods="i"></GoodsItem>
          </li>
        </ul>
        <!-- 加载 -->
       <XtxInfiniteLoading v-model:loading="loading" :finished="finished" @load="onLoad" />
      </div>
    </div>
  </div>
</template>

<script>
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { provide, reactive, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread.vue'
import SubFilter from './sub-filter.vue'
import { findCategoryGoods, findSubCategory } from '@/api/category'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading.vue'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem,
    XtxInfiniteLoading
  },
  setup () {
    const route = useRoute()

    // 默认数据
    const page = ref(1)
    const defaultParams = () => {
      return {
        categoryId: route.params.id, // 二级类别id
        inventory: false, // 是否只显示有库存
        onlyDiscount: false, // 是否只显示打折
        // brandId: '', // 品牌id
        attrs: [], // 选择的属性，object
        sortField: null, // 排序字段  [publishTime,orderNum,evaluateNum,price]
        sortMethod: '', // 排序方式
        // page: 1, // 页码
        pageSize: 5 // 页容量
      }
    }
    // 二级分类 - 商品列表请求参数，
    const reqParams = reactive(defaultParams())
    // 排序参数传给孩儿们
    provide('reqParams', reqParams)

    const subFilter = ref(null)
    // 🔔 watch 监听 id 发生变化，需要重新发送请求
    watch(() => route.params.id, (id) => {
      // 每次换页面 恢复默认数据
      const obj = defaultParams()
      for (const key in obj) {
        reqParams[key] = obj[key]
      }

      // 不是点二级分类 就return
      if (route.path !== `/category/sub/${id}`) return
      // categoryId: '', // 二级类别id
      reqParams.categoryId = id
      findSubCategory(id).then(({ result }) => {
        subFilter.value = result
      })
    }, {
      // 第一次开始监听
      immediate: true
    })
    // 筛选数据 传给孩儿们
    provide('subFilter', subFilter)

    // 发送请求
    const list = ref([])
    const finished = ref(false)
    const loading = ref(false)
    watch(reqParams, async () => {
      console.log(finished.value)
      // 请求参数发送变化，重置加载完毕
      // finished.value = false
      // 切换页面分页回到第一页
      page.value = 1

      const { result } = await findCategoryGoods({ ...reqParams, page: page.value })
      list.value = result.items
    }, { immediate: true })

    // 到底刷新事件

    const onLoad = async () => {
      // 显示加载中
      loading.value = true
      // 加一页
      page.value++
      const { result } = await findCategoryGoods({ ...reqParams, page: page.value })
      // 拿到数据隐藏加载中
      loading.value = false
      list.value.push(...result.items)
      // 判断数据是否已经加载完毕，如果加载完毕就 finished 设置为 true
      if (result.items.length < reqParams.pageSize) {
        // 没有更多了
        finished.value = true
      }
    }

    // 返回给模板
    return { subFilter, list, finished, loading, onLoad }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
